import requests
import pandas as pd

url = "https://capi.tianyancha.com/cloud-intellectual-property/patent/patentListV6"
params = {
    "_": "1731997030695",
    "id": "2392846843",
    "pageSize": "100",
    "pageNum": "1",
    "type": "-100",
    "lprs": "-100",
    "applyYear": "-100",
    "pubYear": "-100",
    "fullSearchText": "",
    "sortField": "",
    "sortType": "-100"
}

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "Host": "capi.tianyancha.com",
    "Origin": "https://www.tianyancha.com",
    "Pragma": "no-cache",
    "Referer": "https://www.tianyancha.com/",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0",
    "X-AUTH-TOKEN": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxODA1Nzk5OTk4NyIsImlhdCI6MTczMTk5NjQ0NiwiZXhwIjoxNzM0NTg4NDQ2fQ.7NPGiG41l0a4y4jlIe5bPmaDXlyilrGSPmQm7WcUwk4juE_bL7zsUXqLNxbP2AZ5849aoxdVaeJt1a5QmS9fww",
    "X-TYCID": "101cf2906c2f11efa348d3df34c32c20",
    "sec-ch-ua": "\"Chromium\";v=\"124\", \"Microsoft Edge\";v=\"124\", \"Not-A.Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "version": "TYC-Web"
}

# 初始化数据列表
data_list = []

page_num = 1
while True:
    params["pageNum"] = str(page_num)
    response = requests.get(url, headers=headers, params=params)
    data = response.json()
    
    items = data['data']['items']
    if not items:
        break  # 如果没有更多数据，退出循环
    
    for item in items:
        data_list.append({
            "relatedCount": item.get("relatedCount"),
            "businessId": item.get("businessId"),
            "title": item.get("title"),
            "patentNum": item.get("patentNum"),
            "uuid": item.get("uuid"),
            "pubnumber": item.get("pubnumber"),
            "applicationTime": item.get("applicationTime"),
            "cat": item.get("cat"),
            "applicantname": item.get("applicantname"),
            "eventTime": item.get("eventTime"),
            "inventor": item.get("inventor"),
            "id": item.get("id"),
            "address": item.get("address"),
            "agency": item.get("agency"),
            "applicantName": item.get("applicantName"),
            "pubDate": item.get("pubDate"),
            "applicationPublishTime": item.get("applicationPublishTime"),
            "lprsLabel_name": item.get("lprsLabel", {}).get("name"),
            "lprsLabel_type": item.get("lprsLabel", {}).get("type"),
            "appnumber": item.get("appnumber"),
            "patentType": item.get("patentType"),
            "imgUrl": item.get("imgUrl"),
            "mainCatNum": item.get("mainCatNum"),
            "createTime": item.get("createTime"),
            "lprs": item.get("lprs"),
            "patentName": item.get("patentName"),
            "applicationPublishNum": item.get("applicationPublishNum"),
            "allCatNum": item.get("allCatNum")
        })
    
    page_num += 1  # 下一页

# 使用pandas将数据保存为Excel文件
df = pd.DataFrame(data_list)

# 字段映射：英文字段名到中文字段名
column_mapping = {
    "relatedCount": "相关数量",
    "businessId": "业务ID",
    "title": "标题",
    "patentNum": "专利号",
    "uuid": "UUID",
    "pubnumber": "公布号",
    "applicationTime": "申请时间",
    "cat": "分类",
    "applicantname": "申请人名称",
    "eventTime": "事件时间",
    "inventor": "发明人",
    "id": "ID",
    "address": "地址",
    "agency": "代理机构",
    "applicantName": "申请人名称",
    "pubDate": "公布日期",
    "applicationPublishTime": "申请公布时间",
    "lprsLabel_name": "LPRS标签名称",
    "lprsLabel_type": "LPRS标签类型",
    "appnumber": "申请号",
    "patentType": "专利类型",
    "imgUrl": "图片URL",
    "mainCatNum": "主分类号",
    "createTime": "创建时间",
    "lprs": "LPRS",
    "patentName": "专利名称",
    "applicationPublishNum": "申请公布号",
    "allCatNum": "所有分类号"
}

# 重命名DataFrame的列
df.rename(columns=column_mapping, inplace=True)

df.to_excel("./pyjd/tianyancha/patent_data.xlsx", index=False)
